package com.next.lottery.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import com.dongfang.utils.ULog;
import com.dongfang.v4.app.DeviceInfo;
import com.next.lottery.R;

/**
 * 
 * @author dongfang
 * 
 */
public class SlidingMenu extends RelativeLayout {

	private View mShowingView;
	// private View mLeftView;
	private View mRightView;
	private RelativeLayout bgShade;
	private int screenWidth;
	private int screenHeight;
	private Context mContext;
	private Scroller mScroller;

	// private VelocityTracker mVelocityTracker;
	// private int mTouchSlop;
	// private float mLastMotionX;
	// private float mLastMotionY;
	// private static final int VELOCITY = 50;
	//
	// private boolean mIsBeingDragged = false;
	// private boolean tCanSlideLeft = false;
	// private boolean tCanSlideRight = false;
	//
	// private boolean hasClickLeft = false;
	// private boolean hasClickRight = false;

	private void init(Context context) {
		mContext = context;
		bgShade = new RelativeLayout(context);
		mScroller = new Scroller(getContext());
		// mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
		screenWidth = DeviceInfo.SCREEN_WIDTH_PORTRAIT;
		screenHeight = DeviceInfo.SCREEN_HEIGHT_PORTRAIT;
		LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight);
		bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		bgShade.setLayoutParams(bgParams);
	}

	public SlidingMenu(Context context) {
		this(context, null);
	}

	public SlidingMenu(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public void addViews(View left, View center, View right) {
		setLeftView(left);
		setRightView(right);
		setCenterView(center);
	}

	public void setLeftView(View view) {
		// LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		// addView(view, behindParams);
		// mLeftView = view;
	}

	public void setRightView(View view) {
		// 设置右侧菜单宽度
		LayoutParams behindParams = new LayoutParams(DeviceInfo.SCREEN_WIDTH_PORTRAIT * 70 / 170,
				LayoutParams.MATCH_PARENT);
		behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		addView(view, behindParams);
		mRightView = view;
	}

	public void setCenterView(View view) {
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

		LayoutParams bgParams = new LayoutParams(screenWidth, screenHeight);
		bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		View bgShadeContent = new View(mContext);
		bgShadeContent.setBackgroundColor(getResources().getColor(R.color.slidingmenu_gb));
		bgShade.addView(bgShadeContent, bgParams);
		addView(bgShade, bgParams);

		addView(view, layoutParams);
		mShowingView = view;
		mShowingView.bringToFront();
	}

	@Override
	public void scrollTo(int x, int y) {
		super.scrollTo(x, y);
		postInvalidate();
	}

	@Override
	public void computeScroll() {
		if (!mScroller.isFinished()) {
			if (mScroller.computeScrollOffset()) {
				int oldX = mShowingView.getScrollX();
				int oldY = mShowingView.getScrollY();
				int x = mScroller.getCurrX();
				int y = mScroller.getCurrY();
				if (oldX != x || oldY != y) {
					if (mShowingView != null) {
						mShowingView.scrollTo(x, y);
						if (x < 0)
							bgShade.scrollTo(x + 20, y);// 背景阴影右偏
						else
							bgShade.scrollTo(x - 20, y);// 背景阴影左偏
					}
				}
				invalidate();
			}
		}
		else {
			if (!isRightViewShow())
				mRightView.setVisibility(View.INVISIBLE);
		}
	}

	private boolean canSlideLeft = false;
	private boolean canSlideRight = false;

	public void setCanSliding(boolean left, boolean right) {
		canSlideLeft = left;
		canSlideRight = right;
	}

	/*
	 * 拦截touch事件
	 */
	// @Override
	// public boolean onInterceptTouchEvent(MotionEvent ev) {
	// final int action = ev.getAction();
	// final float x = ev.getX();
	// final float y = ev.getY();
	// switch (action) {
	// case MotionEvent.ACTION_DOWN:
	// mLastMotionX = x;
	// mLastMotionY = y;
	// mIsBeingDragged = false;
	// if (canSlideRight) { //
	// // mLeftView.setVisibility(View.INVISIBLE);
	// mRightView.setVisibility(View.VISIBLE);
	// }
	// break;
	// case MotionEvent.ACTION_MOVE:
	// final float dx = x - mLastMotionX;
	// final float xDiff = Math.abs(dx);
	// final float yDiff = Math.abs(y - mLastMotionY);
	// if (xDiff > mTouchSlop && xDiff > yDiff) {
	// if (canSlideLeft) {
	// float oldScrollX = mShowingView.getScrollX();
	// if (oldScrollX < 0) {
	// mIsBeingDragged = true;
	// mLastMotionX = x;
	// }
	// else {
	// if (dx > 0) {
	// mIsBeingDragged = true;
	// mLastMotionX = x;
	// }
	// }
	//
	// }
	// else if (canSlideRight) {
	// float oldScrollX = mShowingView.getScrollX();
	// if (oldScrollX > 0) {
	// mIsBeingDragged = true;
	// mLastMotionX = x;
	// }
	// else {
	// if (dx < 0) {
	// mIsBeingDragged = true;
	// mLastMotionX = x;
	// }
	// }
	// }
	//
	// }
	// break;
	//
	// }
	// return mIsBeingDragged;
	// }

	/* 处理拦截后的touch事件 */
	// @Override
	// public boolean onTouchEvent(MotionEvent ev) {
	// if (mVelocityTracker == null) {
	// mVelocityTracker = VelocityTracker.obtain();
	// }
	// mVelocityTracker.addMovement(ev);
	//
	// final int action = ev.getAction();
	// final float x = ev.getX();
	// final float y = ev.getY();
	//
	// switch (action) {
	// case MotionEvent.ACTION_DOWN:
	//
	// // if (!mScroller.isFinished()) { mScroller.abortAnimation(); }
	//
	// mLastMotionX = x;
	// mLastMotionY = y;
	// if (mShowingView.getScrollX() == -getMenuViewWidth() && mLastMotionX < getMenuViewWidth()) {
	// return false;
	// }
	//
	// if (mShowingView.getScrollX() == getDetailViewWidth() && mLastMotionX > getMenuViewWidth()) {
	// return false;
	// }
	//
	// break;
	// case MotionEvent.ACTION_MOVE:
	// if (mIsBeingDragged) {
	// final float deltaX = mLastMotionX - x;
	// mLastMotionX = x;
	// float oldScrollX = mShowingView.getScrollX();
	// float scrollX = oldScrollX + deltaX;
	// if (canSlideLeft) {
	// if (scrollX > 0)
	// scrollX = 0;
	// }
	// if (canSlideRight) {
	// if (scrollX < 0)
	// scrollX = 0;
	// }
	// if (deltaX < 0 && oldScrollX < 0) { // left view
	// final float leftBound = 0;
	// final float rightBound = -getMenuViewWidth();
	// if (scrollX > leftBound) {
	// scrollX = leftBound;
	// }
	// else if (scrollX < rightBound) {
	// scrollX = rightBound;
	// }
	// }
	// else if (deltaX > 0 && oldScrollX > 0) { // right view
	// final float rightBound = getDetailViewWidth();
	// final float leftBound = 0;
	// if (scrollX < leftBound) {
	// scrollX = leftBound;
	// }
	// else if (scrollX > rightBound) {
	// scrollX = rightBound;
	// }
	// }
	// if (mShowingView != null) {
	// mShowingView.scrollTo((int) scrollX, mShowingView.getScrollY());
	// if (scrollX < 0)
	// bgShade.scrollTo((int) scrollX + 20, mShowingView.getScrollY());
	// else
	// bgShade.scrollTo((int) scrollX - 20, mShowingView.getScrollY());
	// }
	//
	// }
	// break;
	// case MotionEvent.ACTION_CANCEL:
	// case MotionEvent.ACTION_UP:
	// if (mIsBeingDragged) {
	// final VelocityTracker velocityTracker = mVelocityTracker;
	// velocityTracker.computeCurrentVelocity(100);
	// float xVelocity = velocityTracker.getXVelocity();// 滑动的速度
	// int oldScrollX = mShowingView.getScrollX();
	// int dx = 0;
	// if (oldScrollX <= 0 && canSlideLeft) {// left view
	// if (xVelocity > VELOCITY) {
	// dx = -getMenuViewWidth() - oldScrollX;
	// }
	// else if (xVelocity < -VELOCITY) {
	// dx = -oldScrollX;
	// if (hasClickLeft) {
	// hasClickLeft = false;
	// setCanSliding(tCanSlideLeft, tCanSlideRight);
	// }
	// }
	// else if (oldScrollX < -getMenuViewWidth() / 2) {
	// dx = -getMenuViewWidth() - oldScrollX;
	// }
	// else if (oldScrollX >= -getMenuViewWidth() / 2) {
	// dx = -oldScrollX;
	// if (hasClickLeft) {
	// hasClickLeft = false;
	// setCanSliding(tCanSlideLeft, tCanSlideRight);
	// }
	// }
	// }
	// if (oldScrollX >= 0 && canSlideRight) {
	// if (xVelocity < -VELOCITY) {
	// dx = getDetailViewWidth() - oldScrollX;
	// }
	// else if (xVelocity > VELOCITY) {
	// dx = -oldScrollX;
	// if (hasClickRight) {
	// hasClickRight = false;
	// setCanSliding(tCanSlideLeft, tCanSlideRight);
	// }
	// }
	// else if (oldScrollX > getDetailViewWidth() / 2) {
	// dx = getDetailViewWidth() - oldScrollX;
	// }
	// else if (oldScrollX <= getDetailViewWidth() / 2) {
	// dx = -oldScrollX;
	// if (hasClickRight) {
	// hasClickRight = false;
	// setCanSliding(tCanSlideLeft, tCanSlideRight);
	// }
	// }
	// }
	// smoothScrollTo(dx);
	// }
	// break;
	// }
	// return true;
	// }

	// private int getMenuViewWidth() {
	// // if (mLeftView == null) {
	// return 0;
	// // }
	// // return mLeftView.getWidth();
	// }
	//
	// private int getDetailViewWidth() {
	// if (mRightView == null) {
	// return 0;
	// }
	// return mRightView.getWidth();
	// }

	void smoothScrollTo(int dx) {
		int duration = 500;
		int oldScrollX = mShowingView.getScrollX();
		mScroller.startScroll(oldScrollX, mShowingView.getScrollY(), dx, mShowingView.getScrollY(), duration);
		invalidate();
	}

	/*
	 * 显示左侧边的view
	 */
	public void showLeftView() {
		/*
		 * int menuWidth = mLeftView.getWidth(); int oldScrollX = mShowingView.getScrollX(); if (oldScrollX == 0) {
		 * mLeftView.setVisibility(View.VISIBLE); // mRightView.setVisibility(View.INVISIBLE);
		 * smoothScrollTo(-menuWidth); tCanSlideLeft = canSlideLeft; tCanSlideRight = canSlideRight; hasClickLeft =
		 * true; setCanSliding(true, false); } else if (oldScrollX == -menuWidth) { smoothScrollTo(menuWidth); if
		 * (hasClickLeft) { hasClickLeft = false; setCanSliding(tCanSlideLeft, tCanSlideRight); } }
		 */
	}

	/* 显示右侧边的view */
	public void showRightView() {
		int menuWidth = mRightView.getWidth();
		int oldScrollX = mShowingView.getScrollX();

		ULog.d("oldScrollX = " + oldScrollX);

		if (oldScrollX == 0) {
			// mLeftView.setVisibility(View.INVISIBLE);
			mRightView.setVisibility(View.VISIBLE);
			smoothScrollTo(menuWidth);
			// tCanSlideLeft = canSlideLeft;
			// tCanSlideRight = canSlideRight;
			// hasClickRight = true;
			// setCanSliding(false, true);
		}
		else if (oldScrollX > 0) {
			smoothScrollTo(-oldScrollX);
			// if (hasClickRight) {
			// hasClickRight = false;
			// setCanSliding(tCanSlideLeft, tCanSlideRight);
			// }
		}
	}

	// 相等表示右侧菜单处于显示状态
	public boolean isRightViewShow() {
		return mShowingView.getScrollX() > 0;
	}

}
